package com.lindroy.androidplayer.ui.find.fragment

import android.os.Bundle
import android.view.View
import com.lindroy.androidplayer.base.BaseListFragment
import com.lindroy.androidplayer.databinding.FragmentQuestionBinding
import com.lindroy.androidplayer.ui.adapter.ArticleAdapter
import com.lindroy.androidplayer.ui.webview.WebViewFragment
import com.lindroy.androidplayer.utils.addSimpleDivider
import com.lindroy.androidplayer.utils.collectListWhenResumed
import com.lindroy.androidplayer.viewmodels.QuestionViewModel

/**
 * @author Lin
 * @date 2022/1/23
 * @function 问答
 * @Description
 */
class QuestionFragment : BaseListFragment<FragmentQuestionBinding, QuestionViewModel>() {

    private lateinit var adapter: ArticleAdapter

    override fun FragmentQuestionBinding.initView(savedInstanceState: Bundle?) {
        vm = mViewMode
        rvQuestion.addSimpleDivider()
        adapter = ArticleAdapter(viewLifecycleOwner)
        rvQuestion.adapter = adapter.withFooter()
        mViewMode.setLayoutStateFlow(adapter.loadStateFlow)
    }

    override fun FragmentQuestionBinding.initListener() {
        mViewMode.getQuestionList().collectListWhenResumed(viewLifecycleOwner) {
            adapter.submitData(it)
        }
        mViewMode.itemClickLiveData.observe(viewLifecycleOwner) {
            WebViewFragment.start(this@QuestionFragment, it.id, it.link, it.title, it.collect)
        }
    }

    override fun onRefresh() {
        adapter.refresh()
    }

    override fun onRetry(view: View) {
        adapter.refresh()
    }

}